package com.ruoyi.video.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.video.domain.VideoHistory;
import com.ruoyi.video.service.IVideoHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 播放历史Controller
 *
 * @author ruoyi
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/video/history")
public class VideoHistoryController extends BaseController {
    @Autowired
    private IVideoHistoryService videoHistoryService;

    /**
     * 查询播放历史列表
     */
    @PreAuthorize("@ss.hasPermi('video:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(VideoHistory videoHistory) {
        startPage();
        List<VideoHistory> list = videoHistoryService.selectVideoHistoryList(videoHistory);
        return getDataTable(list);
    }

    /**
     * 导出播放历史列表
     */
    @PreAuthorize("@ss.hasPermi('video:history:export')")
    @Log(title = "播放历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VideoHistory videoHistory) {
        List<VideoHistory> list = videoHistoryService.selectVideoHistoryList(videoHistory);
        ExcelUtil<VideoHistory> util = new ExcelUtil<VideoHistory>(VideoHistory.class);
        util.exportExcel(response, list, "播放历史数据");
    }

    /**
     * 获取播放历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(videoHistoryService.selectVideoHistoryById(id));
    }


    /**
     * 获取播放历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:history:query')")
    @GetMapping(value = "/getHistoryInfo/{id}")
    public AjaxResult getHistoryInfo(@PathVariable("id") String id) {
        return AjaxResult.success(videoHistoryService.selectVideoHistoryByVideoId(id));
    }

    /**
     * 新增播放历史
     */
    @PreAuthorize("@ss.hasPermi('video:history:add')")
    @Log(title = "播放历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VideoHistory videoHistory) {
        if (StringUtils.isEmpty(videoHistory.getVideoFileId(), videoHistory.getVideoId()) || Objects.isNull(videoHistory.getSecond())) {
            return AjaxResult.noHaveParam();
        }
        int row = videoHistoryService.deleteOldVideoHistory(videoHistory.getVideoId());
        if (StringUtils.isEmpty(videoHistory.getId())) videoHistory.setId(IdUtils.fastUUID());
        videoHistory.setInsertTime(new Date());
        row += videoHistoryService.insertVideoHistory(videoHistory);
        return toAjax(row);
    }

    /**
     * 修改播放历史
     */
    @PreAuthorize("@ss.hasPermi('video:history:edit')")
    @Log(title = "播放历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VideoHistory videoHistory) {
        return toAjax(videoHistoryService.updateVideoHistory(videoHistory));
    }

    /**
     * 删除播放历史
     */
    @PreAuthorize("@ss.hasPermi('video:history:remove')")
    @Log(title = "播放历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(videoHistoryService.deleteVideoHistoryByIds(ids));
    }
}
